package org.glow.changgou.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.user.dao.CitiesMapper;
import org.glow.changgou.user.pojo.Areas;
import org.glow.changgou.user.pojo.Cities;
import org.glow.changgou.user.service.CitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:admin
 * @Description:Cities业务层接口实现类
 * @Date 2022/10/02 21:08:14
 *****/
@Service
public class CitiesServiceImpl extends ServiceImpl<CitiesMapper, Cities> implements CitiesService {

    @Resource
    private CitiesMapper citiesMapper;


    /**
     * Cities条件+分页查询
     * @param cities 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Cities> findPage(Cities cities, int page, int size){
        //分页
        Page<Cities> pageInfo = new Page<>(page,size);
      LambdaQueryWrapper<Cities> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper = getCondition(queryWrapper,cities);
      return citiesMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * Cities分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Cities> findPage(int page, int size){
        Page<Cities> pageInfo = new Page<>(page,size);

        return citiesMapper.selectPage(pageInfo,null);
    }

    /**
     * Cities条件查询
     * @param cities
     * @return
     */
    @Override
    public List<Cities> findList(Cities cities){

        LambdaQueryWrapper<Cities> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,cities);
        return citiesMapper.selectList(queryWrapper);
    }


    /**
     * Cities构建查询对象
     * @param cities
     * @return
     */
    public LambdaQueryWrapper<Cities> getCondition(LambdaQueryWrapper<Cities> queryWrapper, Cities cities){

        if(cities!=null){
            // 城市ID
            if(!StringUtils.isEmpty(cities.getCityid())){
//                    criteria.andEqualTo("cityid",cities.getCityid());
                    queryWrapper.eq(Cities::getCityid,cities.getCityid());
            }
            // 城市名称
            if(!StringUtils.isEmpty(cities.getCity())){
//                    criteria.andEqualTo("city",cities.getCity());
                    queryWrapper.eq(Cities::getCity,cities.getCity());
            }
            // 省份ID
            if(!StringUtils.isEmpty(cities.getProvinceid())){
//                    criteria.andEqualTo("provinceid",cities.getProvinceid());
                    queryWrapper.eq(Cities::getProvinceid,cities.getProvinceid());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        citiesMapper.deleteById(id);
    }

    /**
     * 修改Cities
     * @param cities
     */
    @Override
    public void update(Cities cities){
        citiesMapper.updateById(cities);
    }

    /**
     * 增加Cities
     * @param cities
     */
    @Override
    public void add(Cities cities){
        citiesMapper.insert(cities);
    }

    /**
     * 根据ID查询Cities
     * @param id
     * @return
     */
    @Override
    public Cities findById(String id){
        return  citiesMapper.selectById(id);
    }

    /**
     * 查询Cities全部数据
     * @return
     */
    @Override
    public List<Cities> findAll() {
        return citiesMapper.selectList(null);
    }
}
